<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/23
 * Time: 15:42
 */
 
namespace Modules\Shop\Service;

use HiCommon\Helper;
use HiCommon\Repository\DiscoverRepository;
use HiCommon\Repository\DiscoverZanRepository;
use HiCommon\Repository\DiscoverAppriseRepository;

class DiscoverService
{	


    /**
     *  获取朋友圈列表数据
     *  hinq 2019 10 28
     *
     **/
    public function get_discover_list( array $arr_search ){
        try{

            $arr_option['where'] = ['repairstore_id'=>shop_shop_id() ];
            $arr_option['with'] = ['admin'];
        	//countZan   点赞数统计:只统计朋友圈的点赞数  朋友圈下的评论点赞数不统计
        	$arr_option['withCount'] = ['countZan','countPing'];
            $arr_list = DiscoverRepository::get_list( (array) $arr_option , (array) $arr_search);
            $arr_list->appends($arr_search);//添加链接参数
			$arr_list = list_page_data( $arr_list , $arr_search );
            return hello_success('成功',$arr_list);
        }catch( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


	/**
	 *	验证表单数据
	 *	hinq 2019 10 28
	 *
	 **/
	public function checkdata( array $arr_input ){
		
		if( empty( $arr_input['title'] ) ){
			return ['state'=>1,'msg'=>'标题不能为空','data'=>[],'error'=>''];
		}
		if( strlen( $arr_input['title'] ) < 5 ){
            return ['state'=>1,'msg'=>'标题最少5个字符','data'=>[],'error'=>''];
        }
		if( empty( $arr_input['content'] ) ){
			return ['state'=>1,'msg'=>'内容不能为空','data'=>[],'error'=>''];
		}
		preg_match_all('/<img.*?src="(.*?)".*?>/is',$arr_input['content'],$imgs);
		
		if( count( $imgs[1] ) == 0 ){
			return ['state'=>1,'msg'=>'请上传图片','data'=>[],'error'=>''];
		}else{
			$arr_input['img'] = $imgs[1][0];
        }
		if( empty( $arr_input['pid'] ) ){
			return ['state'=>1,'msg'=>'请选择类型','data'=>[],'error'=>''];
		}
//        if( empty( $arr_input['issue'] ) ){
//            return ['state'=>1,'msg'=>'请选择发布类型','data'=>[],'error'=>''];
//        }
        $arr_input['look_num'] = empty( $arr_input['look_num'] ) ? 0 : (int) $arr_input['look_num'];
		return $arr_input;
	}


    /**
     *	添加朋友圈数据
     *  hinq 2019 10 28
     *
     **/
    public function store( array $arr_input ){
    	try{
    		$arr_input =  self::checkdata( (array) $arr_input );
    		if( $arr_input['state'] == 1 ){
    			return $arr_input;
    		}
    		$arr_input['type'] = 2;   // 2:商户发布
    		$arr_input['status'] = 2;   //2：商户发布的内容需要审核
            $arr_input['author_id'] = shop_user_id();
            $arr_input['repairstore_id'] = shop_shop_id();
			$res = DiscoverRepository::add_one( (array) $arr_input );
			if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('添加成功', $res);
    	}catch(\Exception $exception ){
    		return ['state'=>1,'msg'=>$exception->getMessage()];
    	}
    }


    /**
     *	删除朋友圈
     *	hinq 2019 10 28
     *
     **/
    public function del( int $nId ){
    	try{
    		if(empty( $nId ) ){
    			return hello_error('参数错误');
    		}
    		//验证当前需要删除的数据是否可以删除
    		$where = ['type'=>2,'author_id'=>shop_user_id()  ];
    		$count = DiscoverRepository::get_count_by_where( (array) $where );
    		if( $count == 0 ){
    			return hello_error('您没有权限删除');
    		}
    		$res = DiscoverRepository::delete_one( (int) $nId );
    		if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功', $res);
    	}catch(\Exception $exception ){
    		return ['state'=>1,'msg'=>$exception->getMessage()];
    	}

    }

    /**
     *  获取朋友圈详细信息
     *  hinq 2019 10 29
     *
     **/
    public function detail( array $arr_search ){
        try{
            if( $arr_search['act'] == 'ping' ){ //评论
                 //获取点赞人的列表
                $arr_option['where'] = ['discover_id'=>$arr_search['id'],'lever'=>1];
                $arr_option['field'] = ['id','user_id','created_at','discover_id','content','zan_num'];
                $arr_option['with'] = ['to_user'];
                $arr_list = DiscoverAppriseRepository::get_list( (array) $arr_option );

            }else if( $arr_search['act'] == 'zan'  ){  //点赞
                //获取点赞人的列表
                $arr_option['where'] = ['link_id'=>$arr_search['id'],'type'=>1];
                $arr_option['field'] = ['id','user_id','created_at'];
                $arr_option['with'] = ['to_user'];
                $arr_list = DiscoverZanRepository::get_list( (array) $arr_option );
            }
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data( $arr_list , $arr_search );
            return hello_success('成功',$arr_list);
        }catch( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  获取指定的数据
     *  hinq 2019 12 05
     *
     **/
    public function get_one( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $arr_info = DiscoverRepository::get_one( (int) $nId );
            if( empty( $arr_info->first() ) ){
                return hello_error('参数错误');
            }
            return hello_success('ok',$arr_info->toArray() );
        }catch (\Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  编辑指定的朋友去数据
     *  hinq 2019 12 05
     *
     **/
    public function update( array $arr_input ){
        try{
            $temp = self::checkdata( (array) $arr_input );

            unset( $temp['_token']);
            $admin_id = shop_user_id();
            //验证当前数据是否是操作员的
            $count = DiscoverRepository::get_count_by_where( (array)['author_id'=>$admin_id,'id'=>$arr_input['id']] );
            if( $count == 0 ){
                return hello_error('您没有权限更新数据');
            }
            $res = DiscoverRepository::edit_one( (array) $temp );
            if( empty( $res ) ){
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        }catch (\Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

}